<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input class="input" v-model="form.account" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" type="text"  v-model="form.mobile" placeholder="请输入手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text"  v-model="form.code" placeholder="请输入验证码" />
        <span class="code" @click="sendCode()">发送验证码</span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" type="password"  v-model="form.password" placeholder="请输入密码" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" type="password"  v-model="form.rePassword" placeholder="请确认密码" />
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit()">立即提交</a>
  </div>
</template>

<script>
import { message } from '@/components/Message'
import { reactive } from 'vue'
import { reqUserQQPatchCode, reqUserQQPatchLogin } from '@/api/login'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  props: {
    unionId: {
      type: String
    }
  },
  name: 'CallbackPatch',
  setup (props) {
    const store = useStore()
    const router = useRouter()
    const form = reactive({
      account: '',
      mobile: '',
      code: '',
      password: '',
      rePassword: ''
    })
    const sendCode = async () => {
      // 先校验手机号
      if (!/^1[3-9]\d{9}$/.test(form.mobile)) message({ text: '手机号码格式不正确' })
      const { result } = await reqUserQQPatchCode(form.mobile)
      console.log('校验成功', result)
    }
    // 提交注册
    const submit = async () => {
      console.log('唯一表示', form, props.unionId)
      const { result } = await reqUserQQPatchLogin({
        unionId: props.unionId,
        ...form
      })
      console.log('注册成功', result)
      store.commit('user/setprofile', result)
      router.push('/')
    }

    return { form, sendCode, submit }
  }

}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
